package com.kun.controller;

import com.kun.entity.ResponseJson;
import com.kun.entity.SuppInfo;
import com.kun.entity.UserInfo;
import com.kun.entity.WorkInfo;
import com.kun.service.WorkInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("/work")
public class WorkController {
    @Autowired
    private WorkInfoService workInfoService;
    @GetMapping("/work/{workId}")
    public ResponseJson<WorkInfo> findById(@PathVariable("workId")Integer workId){
        WorkInfo workInfo =workInfoService.findById(workId);
        ResponseJson<WorkInfo> result = new ResponseJson<>();
        if(workInfo == null){
            result.setCode(500);
            result.setMsg("未找到数据");
        }else{
            result.setCode(200);
            result.setMsg("找到数据");
            result.setData(workInfo);
        }
        return result;
    }
    @GetMapping("/findByPage")
    public ResponseJson<Map> findByPages(@RequestParam("page") Integer pageNum
            , @RequestParam("limit") Integer pageSize
            , WorkInfo workInfo){
        Map<String, Object> data = workInfoService.selectByPage(pageNum, pageSize,workInfo);
        ResponseJson<Map> result = new ResponseJson<>();
        if (data != null){
            result.setCode(200);
            result.setMsg("查询成功");
            result.setData(data);
        }else {
            result.setCode(500);
            result.setMsg("查询失败");
        }
        return result;
    }
    @PostMapping("/insert")
    public ResponseJson<WorkInfo> insertSupp(WorkInfo workInfo){
        ResponseJson<WorkInfo> result = new ResponseJson<>();
        int ui =workInfoService.insertWork(workInfo);
        if (ui == 0){
            result.setCode(500);
            result.setMsg("添加失败");
        }
        result.setCode(200);
        result.setMsg("添加成功");
        return result;
    }
    @GetMapping("/findWork")
    public ResponseJson<List> findWork(){
        ResponseJson<List> result = new ResponseJson<>();
        List<WorkInfo> data = workInfoService.findWork();
        if (data!=null){
            result.setCode(200);
            result.setMsg("查询成功");
            result.setData(data);
        }else {
            result.setCode(500);
            result.setMsg("查询失败");
        }
        return result;

    }
    @DeleteMapping("/batchDelete")
    public ResponseJson batchDelete(Integer[] workIds){
        ResponseJson<WorkInfo> result = new ResponseJson<>();
        if (workInfoService.deleteWork(workIds)>0){
            result.setCode(200);
            result.setMsg("删除成功");
        }else{
            result.setCode(500);
            result.setMsg("删除失败");
        }
        return result;
    }
    @PutMapping("/updateWork")
    public ResponseJson updateUser(WorkInfo workInfo){
        ResponseJson<WorkInfo> result = new ResponseJson<>();
        if (workInfoService.updateWork(workInfo)>0){
            result.setCode(200);
            result.setMsg("更新成功");
        }else{
            result.setCode(500);
            result.setMsg("更新失败");
        }
        return result;
    }
}
